# -*- coding: utf-8 -*-
import json
import os
from tqdm import tqdm


def convert_label_json(json_dir, save_dir, classes):
    jsons_path = os.listdir(json_dir)

    for json_path in tqdm(jsons_path):
        # for json_path in json_paths:
        path = os.path.join(json_dir, json_path)
        with open(path, 'r') as load_f:
            json_dict = json.load(load_f)
        h, w = json_dict['imageHeight'], json_dict['imageWidth']

        # save txt path
        txt_path = os.path.join(save_dir, json_path.replace('json', 'txt'))
        txt_file = open(txt_path, 'w')

        for shape_dict in json_dict['shapes']:
            label = shape_dict['label']
            label_index = classes.index(label)
            points = shape_dict['points']

            points_nor_list = []

            for point in points:
                points_nor_list.append(point[0] / w)
                points_nor_list.append(point[1] / h)

            points_nor_list = list(map(lambda x: str(x), points_nor_list))
            points_nor_str = ' '.join(points_nor_list)

            txt_file.write(f"{label_index} {points_nor_str}" + "\n")

        txt_file.close()


if __name__ == "__main__":
    json_dir = r"./data\annotations"
    save_dir = r"./data\labels"
    # classes = ["cat", "dog"]
    classes = ["mt", "mgs"]

    convert_label_json(json_dir, save_dir, classes)
